import pygame
import random
from settings import screen_width, screen_height
import random



class PowerUp(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image =  pygame.image.load("道具.jpg").convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(screen_width - self.rect.width)
        self.rect.y = -self.rect.height  # 从屏幕顶部开始掉落

    def update(self):
        self.rect.y += 5  # 道具向下移动
        if self.rect.top > screen_height:
            self.kill()
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.radius = 25
        self.image = pygame.image.load("玩家.png").convert_alpha()  # 替换为实际的玩家图片路径
        self.rect = self.image.get_rect()
        self.rect.center = (screen_width // 2, screen_height - 50)  # 初始位置
        self.power_up = False
        self.power_up_time = 0

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.rect.x -= 5
        if keys[pygame.K_RIGHT]:
            self.rect.x += 5
        if keys[pygame.K_UP]:
            self.rect.y -= 5
        if keys[pygame.K_DOWN]:
            self.rect.y += 5

        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > screen_width:
            self.rect.right = screen_width
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > screen_height:
            self.rect.bottom = screen_height

        # 道具效果持续时间检查
        if self.power_up and pygame.time.get_ticks() - self.power_up_time > 10000:  # 10秒后取消道具效果
            self.power_up = False

    def shoot(self, bullets_group, all_sprites_group):
        if self.power_up:
            # 发射四列子弹
            positions = [self.rect.centerx - 30, self.rect.centerx - 10, self.rect.centerx + 10, self.rect.centerx + 30]
            for pos in positions:
                bullet = Bullet(pos, self.rect.top)
                bullets_group.add(bullet)
                all_sprites_group.add(bullet)
        else:
            # 发射单列子弹
            bullet = Bullet(self.rect.centerx, self.rect.top)
            bullets_group.add(bullet)
            all_sprites_group.add(bullet)

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("敌人.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.radius = 25
        self.rect.x = random.randint(0, screen_width - self.rect.width)
        self.rect.y = random.randint(-100, -40)
        self.speedy = random.randint(1, 8)


    def update(self):
        self.rect.y += self.speedy
        if self.rect.top > screen_height + 10:
            self.rect.x = random.randint(0, screen_width - self.rect.width)
            self.rect.y = random.randint(-100, -40)
            self.speedy = random.randint(1, 8)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.radius = 5
        self.image = pygame.image.load("子弹.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.bottom = y
        self.speedy = -10

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 0:
            self.kill()
